/**
 * 航点相关工具函数
 */

/**
 * 创建默认航点记录
 * @param {Object} coordinate - 坐标信息
 * @param {string} uavId - 无人机ID
 * @param {string} groundId - 地面点ID
 * @param {number} index - 索引
 * @returns {Object} 航点记录对象
 */
export function createDefaultWaypointRecord(coordinate, uavId, groundId, index) {
  return {
    'uavId': uavId,
    "is_risky": 0,
    "point_index": index,
    "point_name": `航点${index}`,
    "point_type": 0,
    "point_gimbal_pitch_angle": 0.0,
    "point_coordinate": coordinate,
    "point_turn_type": 2,
    "point_heading_type": 2,
    "point_fly_speed": 10,
    "point_fly_height": coordinate.altitude || 100,
    "point_actions": [
      {
        "action_type": 40,
        "point_ground": {
          longitude: coordinate.longitude,
          latitude: coordinate.latitude,
          altitude: 0,
        },
        "groundId": groundId
      }
    ],
  }
}

/**
 * 创建默认动作记录
 * @param {Object} groundCoordinate - 地面坐标
 * @param {string} groundId - 地面点ID
 * @returns {Object} 动作记录对象
 */
export function createDefaultActionRecord(groundCoordinate, groundId) {
  return {
    "action_type": 40,
    "point_ground": groundCoordinate,
    "groundId": groundId
  }
}

/**
 * 验证航点坐标
 * @param {Object} coordinate - 坐标对象
 * @returns {boolean} 是否有效
 */
export function validateWaypointCoordinate(coordinate) {
  if (!coordinate) return false
  
  const { longitude, latitude } = coordinate
  
  // 检查经纬度范围
  if (longitude < -180 || longitude > 180) return false
  if (latitude < -90 || latitude > 90) return false
  
  return true
}

/**
 * 格式化坐标显示
 * @param {Object} coordinate - 坐标对象
 * @param {number} precision - 精度
 * @returns {string} 格式化后的坐标字符串
 */
export function formatCoordinate(coordinate, precision = 6) {
  if (!coordinate) return '0.000000, 0.000000'
  
  const { longitude, latitude } = coordinate
  return `${longitude.toFixed(precision)}, ${latitude.toFixed(precision)}`
}

/**
 * 更新航点索引
 * @param {Array} recordList - 记录列表
 */
export function updateWaypointIndexes(recordList) {
  recordList.forEach((record, index) => {
    record.point_index = index
    record.point_name = `航点${index + 1}`
  })
}

/**
 * 查找航点记录
 * @param {Array} recordList - 记录列表
 * @param {string} uavId - 无人机ID
 * @returns {Object|null} 找到的记录或null
 */
export function findWaypointRecord(recordList, uavId) {
  return recordList.find(record => record.uavId === uavId) || null
}

/**
 * 查找航点记录索引
 * @param {Array} recordList - 记录列表
 * @param {string} uavId - 无人机ID
 * @returns {number} 索引，未找到返回-1
 */
export function findWaypointRecordIndex(recordList, uavId) {
  return recordList.findIndex(record => record.uavId === uavId)
}

/**
 * 删除航点记录
 * @param {Array} recordList - 记录列表
 * @param {string} uavId - 无人机ID
 * @returns {boolean} 是否删除成功
 */
export function removeWaypointRecord(recordList, uavId) {
  const index = findWaypointRecordIndex(recordList, uavId)
  if (index !== -1) {
    recordList.splice(index, 1)
    updateWaypointIndexes(recordList)
    return true
  }
  return false
}

/**
 * 交换航点位置
 * @param {Array} recordList - 记录列表
 * @param {number} index1 - 第一个索引
 * @param {number} index2 - 第二个索引
 */
export function swapWaypointPositions(recordList, index1, index2) {
  if (index1 >= 0 && index1 < recordList.length && 
      index2 >= 0 && index2 < recordList.length) {
    [recordList[index1], recordList[index2]] = [recordList[index2], recordList[index1]]
    updateWaypointIndexes(recordList)
  }
}

/**
 * 获取航点统计信息
 * @param {Array} recordList - 记录列表
 * @returns {Object} 统计信息
 */
export function getWaypointStatistics(recordList) {
  const totalWaypoints = recordList.length
  const actionCounts = {}
  
  recordList.forEach(record => {
    record.point_actions.forEach(action => {
      actionCounts[action.action_type] = (actionCounts[action.action_type] || 0) + 1
    })
  })
  
  return {
    totalWaypoints,
    actionCounts,
    averageActionsPerWaypoint: totalWaypoints > 0 ? 
      recordList.reduce((sum, record) => sum + record.point_actions.length, 0) / totalWaypoints : 0
  }
}

/**
 * 验证航点动作
 * @param {Object} action - 动作对象
 * @returns {boolean} 是否有效
 */
export function validateWaypointAction(action) {
  if (!action) return false
  
  const { action_type, action_param_value } = action
  
  // 检查动作类型
  if (typeof action_type !== 'number') return false
  
  // 根据动作类型验证参数
  switch (action_type) {
    case 6: // 转动云台
      return typeof action_param_value === 'number' && 
             action_param_value >= -180 && action_param_value <= 180
    case 40: // 地面点
      return action.point_ground && 
             validateWaypointCoordinate(action.point_ground)
    default:
      return true
  }
}

/**
 * 创建航点动作选项
 * @returns {Array} 动作选项数组
 */
export function createActionOptions() {
  return [
    { value: 0, label: "拍照" },
    { value: 1, label: "开始录像" },
    { value: 2, label: "结束录像" },
    { value: 6, label: "转动云台" },
    { value: 7, label: "变焦值调整" },
    { value: 8, label: "时间间隔" },
    { value: 9, label: "距离间隔" },
    { value: 12, label: "探照灯开启" },
    { value: 13, label: "探照灯关闭" },
    { value: 40, label: "地面点设置" }
  ]
}

/**
 * 创建飞行模式选项
 * @returns {Array} 飞行模式选项数组
 */
export function createFlightModeOptions() {
  return [
    { value: 1, label: '直线飞行（过点且到点停）' },
    { value: 2, label: '协调转弯（提前转弯不过点）' },
    { value: 3, label: '曲线飞行（到点停）' },
    { value: 4, label: '曲线飞行（不到点停）' }
  ]
}

/**
 * 创建航向模式选项
 * @returns {Array} 航向模式选项数组
 */
export function createHeadingModeOptions() {
  return [
    { value: 0, label: '自动' },
    { value: 1, label: '锁定' },
    { value: 2, label: '遥控器控制' },
    { value: 3, label: '云台依照航点方向旋转' },
    { value: 4, label: '朝向兴趣点' },
    { value: 5, label: '飞机和云台的偏航同时旋转' }
  ]
}
